/*
 * Created on :Jul 23, 2012 Author :songlin.li
 */
package com.dmagic.cms.common.util.reflection;

import java.lang.reflect.Method;

/**
 * [ticket id] get bean filed value
 * 
 * @author songlin.li @ Version : V<Ver.No> <Jul 23, 2012>
 */
public class BeanParser {

    private static String getMethodName(String property, String prefix) {
        String prop = Character.toUpperCase(property.charAt(0)) + property.substring(1);
        String methodName = prefix + prop;
        return methodName;
    }

    private static Object parse(Object bean, String expr) {
        Class beanClass = bean.getClass();
        Method method = null;
        Object result = null;

        try {
            method = beanClass.getMethod(getMethodName(expr, "get"), new Class[] {});
            result = method.invoke(bean, new Object[] {});
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return result;
    }

    public static Object doParse(Object bean, String expr) {
        String keys[] = expr.split("\\.");
        Object obj = null;
        for (int i = 1; i < keys.length; i++) {
            obj = parse(bean, keys[i]);
            bean = obj;
        }
        return obj;
    }
}
